/**
 * @file hal_iic.h
 * @author liufeng (liufeng@injoinic.com)
 * @brief hardware iic with LL library.
 * @version 0.1
 * @date 2024-11-14
 *
 * @copyright Copyright (c) 2024  injoinic
 *
 */

#ifndef __HAL_IIC_H__
#define __HAL_IIC_H__

#include "stddef.h"
#include "stdint.h"
#include "stm32f4xx_ll_dma.h"
#include "stm32f4xx_ll_i2c.h"

#define I2C_MEMADD_SIZE_8BIT  0
#define I2C_MEMADD_SIZE_16BIT 1

typedef struct
{
    I2C_TypeDef* Instance; /*!< I2C registers base address */
    DMA_TypeDef* dmatx;    /*!< I2C Tx DMA handle parameters */
    uint32_t dmatx_stream; /* LL_DMA_STREAM_x */
    DMA_TypeDef* dmarx;    /*!< I2C Rx DMA handle parameters */
    uint32_t dmarx_stream; /* LL_DMA_STREAM_x */
} I2C_HandleTypeDef;

/* export i2c_handle -------------------------------------------------------- */

extern I2C_HandleTypeDef i2c1_handle;

/* export i2c function ------------------------------------------------------ */

uint8_t hal_i2c_transmit(
    I2C_HandleTypeDef* hi2c, uint16_t SlaveAddr, uint8_t* pData, uint16_t Size, uint32_t Timeout
);
uint8_t hal_i2c_receive(
    I2C_HandleTypeDef* hi2c, uint16_t SlaveAddr, uint8_t* pData, uint16_t Size, uint32_t Timeout
);
uint8_t hal_i2c_mem_write(
    I2C_HandleTypeDef* hi2c,
    uint16_t SlaveAddr,
    uint16_t MemAddress,
    uint16_t MemAddSize,
    uint8_t* pData,
    uint16_t Size,
    uint32_t Timeout
);
uint8_t hal_i2c_mem_read(
    I2C_HandleTypeDef* hi2c,
    uint16_t SlaveAddr,
    uint16_t MemAddress,
    uint16_t MemAddSize,
    uint8_t* pData,
    uint16_t Size,
    uint32_t Timeout
);
uint8_t hal_i2c_transmit_dma(
    I2C_HandleTypeDef* hi2c, uint16_t SlaveAddr, uint8_t* pData, uint16_t Size
);
uint8_t hal_i2c_receive_dma(
    I2C_HandleTypeDef* hi2c, uint16_t SlaveAddr, uint8_t* pData, uint16_t Size
);
uint8_t hal_i2c_mem_write_dma(
    I2C_HandleTypeDef* hi2c,
    uint16_t SlaveAddr,
    uint16_t MemAddress,
    uint16_t MemAddSize,
    uint8_t* pData,
    uint16_t Size
);
uint8_t hal_i2c_mem_read_dma(
    I2C_HandleTypeDef* hi2c,
    uint16_t SlaveAddr,
    uint16_t MemAddress,
    uint16_t MemAddSize,
    uint8_t* pData,
    uint16_t Size
);

#endif /* __HAL_IIC_H__ */
